let screeps = {
    //群号
    screepsGroupQQNumber: "565401831",
    //Storage的储存key
    storageKey: "luerdog:screeps-autojs",
    //Screeps的token保存在Storage中的key
    tokenKey: "token",
    //我的房间
    myRoomsKey: "myRooms",
    //是否需要保存统计数据
    needSaveMemory: true,
    //统计数据保存位置,
    saveDir: "./data/",
    //服务端地址 自己使用/server/app.js搭建服务后,可以使用自己的ip地址
    serverAddress: "http://211.149.141.88:8181/",
    //Console网址
    consoleUrl: "https://hopgoldy.github.io/screeps-commander/",
    //触发函数
    actions: [
        {
            name: "获取Token",
            func: function (screep) {
                token = screep.getToken();
                toast("token已复制到剪切板");
                setClip(token);
            }
        },
        {
            name: "Gete",
            func: function (screep) {
                room = screep.selectRoom();
                toast("gete 模版已复制到剪切板~");
                setClip('Game.rooms.' + room + '.gete()')
            }
        },
        {
            name: "Pute",
            func: function (screep) {
                room = screep.selectRoom();
                toast("pute 模版已复制到剪切板~");
                setClip('Game.rooms.' + room + '.pute()');
            }
        },
        {
            name: "统计数据",
            func: function (screep) {
                let shards = ["shard2",'shard3'];
                let shard = dialogs.select("请选择一个Shard",shards);
                screep.showMemoryData(shards[shard]);
            }
        },
        {
            name: "打开Console",
            func: function (screep) {
                app.openUrl(screep.consoleUrl);
            }
        },
        {
            name: "重设Token",
            func: function (screep) {
                let storage = storages.create(screep.storageKey);
                storage.remove(screep.tokenKey);
                getToken();
            }
        },
        {
            name: "收Cr",
            func: function (screep) {
                app.startActivity({
                    action: "android.intent.action.VIEW",
                    data: "mqq://im/chat?chat_type=group&version=1&src_type=web&uin=" + screep.screepsGroupQQNumber,
                    packageName: "com.tencent.mobileqq",
                });
                setClip('收CR,有的大佬可以直接@我或者加我好友,谢谢!')
                toast('内容已复制到剪贴板')
            }
        },
        {
            name:"简单指令",
            func:function(screep){
                screep.selectComment();
            }
        },
        {
            name: "清除日志",
            func: function (screep) {
                files.removeDir('./data');
                files.ensureDir(logFile)
            }
        }
    ],

    //获取screeps的Token
    getToken: function () {
        let storage = storages.create(this.storageKey)
        let token = null;
        if (storage.contains(this.tokenKey)) {
            token = storage.get(this.tokenKey);
        } else {
            token = dialogs.rawInput("请输入您的token");
            if (token == null) {
                toast("Token设置失败,请重试!");
            } else {
                storage.put(this.tokenKey, token);
                toast('Token设置成功~');
            }
        }
        return token;
    },
    //选择房间
    selectRoom: function () {
        myRooms = this.getMyRoomsFromStorage();
        let room = dialogs.select("请选择一个房间", myRooms);
        return myRooms[room];
    },
    //选择常用命令
    selectComment: function () {
        let comments = [
            "Game.cpu.getUsed()",
            "Memory.showCpuUsed = true",
            "Memory.showCpuUsed = false",
        ];
        let comment = dialogs.select("请选择指令", comments);
        setClip(comments[comment])
        toast("指令已复制到剪切板")
    },
    //从Storage获取游戏房间
    getMyRoomsFromStorage: function () {
        let storage = storages.create(this.storageKey);
        if (storage.contains(this.myRoomsKey)) {
            myRooms = JSON.parse(storage.get(this.myRoomsKey));
        } else {
            toast('请先运行数据统计,缓存房间信息');
        }
        return myRooms
    },
    //保存游戏房间到Storage
    setMyRoomsToStroage: function (rooms) {
        let storage = storages.create(this.storageKey);
        let jsonStr = JSON.stringify(rooms);
        storage.put(this.myRoomsKey, jsonStr);
    },
    //展示统计数据
    showMemoryData: function (shard) {
        let token = this.getToken();
        let memoryAPI = this.serverAddress + "?token=" + token + "&type=memory&key=stats&shard="+shard;
        let response = http.get(memoryAPI);
        let data = response.body.json()['data'];
        console.show();
        console.log(new Date);

        console.info('房间数据:');
        let rooms = data['rooms'];
        //清空房间数据
        let myRooms = [];
        for (let room in rooms) {
            myRooms.push(room)
            console.info(
                "房间:%s 等级:%d 进度:%s 库存:%s",
                room,
                rooms[room].controllerLevel,
                rooms[room].controllerRatio == null ? "顶级无疑" : rooms[room].controllerRatio.toFixed(2) + '%',
                rooms[room].energy
            );
        }
        this.setMyRoomsToStroage(myRooms);
        console.info('GCl数据 等级:%s 进度:%s', data['gclLevel'], data['gcl'].toFixed(2) + '%');

        console.info('GPl数据 等级:%s 进度:%s', data['gplLevel'], data['gpl'].toFixed(2) + "%");
        console.info('CPU使用率:%s', data['cpu'].toFixed(2));
        console.info("当前CR余额:", data['credit']);
        console.info("CR近kt盈亏", data['profit']);
        console.info("当前Pixel:", data['pixel']);
    },
    //展示选择框
    showDialogs: function () {
        let options = [];
        for (let a of this.actions) {
            options.push(a.name);
        }

        let opt = dialogs.select("请选择一个命令", options);
        console.hide();
        if (this.actions[opt]) {
            this.actions[opt].func(this);
        } else {
            toast('请重新选择!');
        }
    },
}

let logFile = "./data/" + (new Date).toString().split(' GMT')[0] + ".txt";
files.ensureDir(logFile);
console.setGlobalLogConfig({
    "file": logFile,
});
console.setSize(device.width, device.height / 1.5);
screeps.showDialogs();